/**
 * Project  : Looker
 * Package  : net.freestu.looker.monitor
 * File     : ListParser.java
 * Author   : solosky < solosky772@qq.com >
 * Date     : Aug 27, 2009
 * Modified : Aug 27, 2009
 * License  : Apache License 2.0 
 */
package net.freestu.looker.monitor;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.freestu.looker.common.Category;
import net.freestu.looker.common.NewsMeta;


/**
 * 列表分析
 * 把原始的HTML分析为一个新闻列表  
 */
public class ListParser extends AbstractParser
{
	
	/**
	 * 分析出来的新闻信息列表
	 */
	private ArrayList<NewsMeta> newsMetaList;
	/**
	 * 构造函数
     * @param html
     * @param siteConfiguration
     */
    public ListParser(String html, Category category)
    {
	    super(html, category);
	    newsMetaList = new ArrayList<NewsMeta>();
    }

	/* (non-Javadoc)
	 * @see net.freestu.looker.monitor.AbstractParser#getParsed()
	 */
	@Override
	public Object getParsed()
	{
		return this.newsMetaList;
	}

	/* (non-Javadoc)
	 * @see net.freestu.looker.monitor.AbstractParser#parse()
	 */
	@Override
	public void parse()
	{
		//建立提取标题和新闻ID的正则表达式
		Pattern pt = Pattern.compile(siteConfiguration.getString("list.patterns.news"));
		Matcher mc = pt.matcher(html);
		NewsMeta oneNews = null;
		String url = siteConfiguration.getString("site.url")+siteConfiguration.getString("view.url");
		//循环提取其中的新闻
		while (mc.find()){
			oneNews = new NewsMeta();
			String newsID = mc.group(1);
			oneNews.setUrl(url.replace("{newsid}",newsID ));
			oneNews.setCategory(category);
			newsMetaList.add(oneNews);
		}
	}
}
